﻿using Microsoft.SharePoint;
using System.Collections.Generic;

namespace ServBus.ListForm2010.Common.XSNOperator
{

    #region Abstract Class

    public abstract class BaseListInfo
    {
        #region Accessor

        public string ListTitle { get; set; }

        public string UniqueName { get; set; }

        public string ListID { get; set; }

        public List<BaseContentTypeInfo> ContentTypeInfoList { get; set; }

        #endregion
    }

    public class XListInfo : BaseListInfo
    {
        public XListInfo()
        {
            ContentTypeInfoList = new List<BaseContentTypeInfo>();
        }

        public void GenerateListInfo(SPList p_ListInfo)
        {
            ListID = p_ListInfo.ID.ToString();
            ListTitle = p_ListInfo.Title;
            UniqueName = p_ListInfo.Title.Replace(" ", "_");
            UniqueName = ServBus.ListForm2010.Common.XSNOperator.FieldHelper.GetUniquefieldName(UniqueName);
        }

        public void AddContentTypeInfo(SPContentType p_ContentType)
        {
            ContentTypeInfo cTypeInfo = new ContentTypeInfo
            {
                ContentTypeId = p_ContentType.Id.ToString(),
                ContentTypeName = p_ContentType.Name,
                UniqueName = ServBus.ListForm2010.Common.XSNOperator.FieldHelper.GetUniquefieldName((ListTitle + "_" + p_ContentType.Name).Replace(" ", "_"))
            };

            foreach (SPField sField in p_ContentType.Fields)
            {
                if (!sField.Hidden && !sField.Group.Equals("_Hidden"))
                {
                    XFieldInfo sFieldInfo = FieldInfoFactory.GetFieldInfoDefinition(sField, p_ContentType.Name);
                    if (sFieldInfo != null)
                    {
                        cTypeInfo.FieldInfoList.Add(sFieldInfo);
                        if (sField.ShowInDisplayForm == null)
                        {
                            cTypeInfo.ViewFieldList.Add(sFieldInfo);
                        }
                        else
                        {
                            bool showInDispForm = sField.ShowInDisplayForm ?? false;
                            if (showInDispForm)
                            {
                                cTypeInfo.ViewFieldList.Add(sFieldInfo);
                            }
                        }

                    }
                }
            }
            ContentTypeInfoList.Add(cTypeInfo);
        }
    }

    #endregion
}